///////////////////////////////////////////////////////////////////////
//	File	:	"CMenuEngine.cpp"
//
//	Author	:	James Gonzalez (JG)
//
//	Purpose	:	This class will load and display the screens for the game
///////////////////////////////////////////////////////////////////////

#include "CMenuEngine.h"
#include "../Wrappers/CSGD_TextureManager.h"
#include "../xml/tinyxml.h"

CMenuEngine::CMenuEngine(void)
{
	m_pTM = CSGD_TextureManager::GetInstance();
	m_nMenuItemCount = 0;
	m_nObjectCount = 0;
	m_nPositionCount = 0;

}

CMenuEngine::~CMenuEngine(void)
{
	
}



bool CMenuEngine::LoadScreen(const char* szFileName)
{

	TiXmlDocument xmlDoc;

	if (!xmlDoc.LoadFile(szFileName))
		return false;


	TiXmlElement* pRoot = xmlDoc.RootElement();
	TiXmlElement* pElement	= xmlDoc.RootElement();
	int numObjects = 0; 
	pElement->Attribute("NumObjects",&numObjects);
	int numMenuItems = 0;
	pElement->Attribute("NumMenuItems",&numMenuItems);
	int numPositions = 0;
	pElement->Attribute("NumPositions",&numPositions);



	pElement	= pRoot->FirstChildElement("Object");
	for(int o = 0; o < numObjects; o++)
	{

		tObjects obj;	
		sprintf_s(obj.m_szName,"%s",pElement->Attribute("name"));
		pElement->Attribute("xpos",&obj.m_nX );
		pElement->Attribute("ypos",&obj.m_nY );
		obj.m_nImage =  m_pTM->LoadTexture( (char*)pElement->Attribute("ImagePath") );
		m_vAllObjects.push_back(obj); 

		pElement = pElement->NextSiblingElement("Object");



	}
	pElement = pRoot->FirstChildElement("MenuItem");
 	for(int m = 0; m < numMenuItems; m++)
 	{
 
 		tMenuItems obj;	
 		sprintf_s(obj.m_szName,"%s",pElement->Attribute("name"));
 		pElement->Attribute("xpos",&obj.m_nX );
 		pElement->Attribute("ypos",&obj.m_nY );
 		m_vAllMenuItmes.push_back(obj); 
 
 		pElement = pElement->NextSiblingElement("MenuItem");
 	}

	pElement	= pRoot->FirstChildElement("Position");
	for(int p = 0; p < numPositions; p++)
	{

		tPositions obj;	
		sprintf_s(obj.m_szName,"%s",pElement->Attribute("name"));
		pElement->Attribute("xpos",&obj.m_nX );
		pElement->Attribute("ypos",&obj.m_nY );
		m_vAllPositions.push_back(obj); 

		pElement = pElement->NextSiblingElement("Position");
	}


	
	return true;
}

void CMenuEngine::RenderScreen()
{
	for (unsigned int i = 0; i < m_vAllObjects.size(); i++)
	{
		m_pTM->Draw(m_vAllObjects[i].m_nImage,m_vAllObjects[i].m_nX,m_vAllObjects[i].m_nY,1.0f,1.0f,NULL,0.0f,0.0f,0.0f,0xFFFFFFFF);
	}
}




int CMenuEngine::GetMenuMembers(char * szMenuName,int nMember)
{
	for(unsigned int i = 0; i < m_vAllMenuItmes.size(); i++)
	{
		if ( 0 == strcmp(m_vAllMenuItmes[i].m_szName ,szMenuName) )
		{
			switch(nMember)
			{
			case XPOS:
				return m_vAllMenuItmes[i].m_nX;
			case YPOS:
				return m_vAllMenuItmes[i].m_nY;
			case MENUID:
				return m_vAllMenuItmes[i].m_nID;
			}
		}
	}
	return -1;
	
}



int CMenuEngine::GetPosObjMembers(char * szMenuName,int nMember)
{

	for(unsigned int i = 0; i < m_vAllPositions.size(); i++)
	{
		if ( 0 == strcmp(m_vAllPositions[i].m_szName ,szMenuName) )
		{
			switch(nMember)
			{
			case XPOS:
				return m_vAllPositions[i].m_nX;
			case YPOS:
				return m_vAllPositions[i].m_nY;
			}


		}
	}
	return -1;
}


int CMenuEngine::GetObjectMembers(char * szMenuName,int nMember)
{

	for(unsigned int i = 0; i < m_vAllPositions.size(); i++)
	{
		if ( 0 == strcmp(m_vAllPositions[i].m_szName ,szMenuName) )
		{
			switch(nMember)
			{
			case XPOS:
				return m_vAllObjects[i].m_nX;
			case YPOS:
				return m_vAllObjects[i].m_nY;
			}
		}
	}
	return -1;
}



void CMenuEngine::ExitScreen()
{
	m_vAllMenuItmes.clear();
	m_vAllObjects.clear();
	m_vAllPositions.clear();
	m_nMenuItemCount = 0;
	m_nObjectCount = 0;
	m_nPositionCount = 0;
}